package com.houtai.service.black;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.houtai.base.RetInfo;
import com.houtai.bean.TdBlack;
import com.houtai.bean.TdCompany;
import com.houtai.bean.User;
import com.houtai.bean.dto.balck.TdBlackListDto;
import com.houtai.common.util.LogUtils;
import com.houtai.dao.TdBlackDao;
import com.houtai.dao.TdCompanyDao;
import com.houtai.dao.TsJzAdminOperationDao;
import com.houtai.dao.UserDao;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class BlackService {
    private static Logger logger = Logger.getLogger(BlackService.class);
    @Autowired
    private TdBlackDao tdBlackDao;
    @Autowired
    private TdCompanyDao tdCompanyDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private TsJzAdminOperationDao tsJzAdminOperationDao;

    /**
     * @Purpose   查找黑名单
     * @param     page
     * @version   1.0
     * @author    李准
     * @time      2017-08-19
     * @return    RetInfo
     */
    public RetInfo findTdBlackList(TdBlackListDto page, HttpServletRequest request) {
        String logInfo = this.getClass().getName() + ":findTdBlackList:";
        RetInfo retInfo = new RetInfo();
        logger.info("======" + logInfo + "begin======");
        try {
            PageHelper.startPage(page.getCurrent_page(), page.getPage_size(), "black_id desc");
            List<TdBlack> lists = tdBlackDao.findTdBlackList(page);
            PageInfo<TdBlack> list = new PageInfo<>(lists);
            Map<String, Object> map = new HashMap<>();
            map.put("list", list.getList());
            map.put("total_page", list.getPages());
            map.put("total_record", list.getTotal());
            map.put("current_page", list.getPageNum());
            map.put("page_size", list.getPageSize());
            map.put("black_object_id", page.getBlack_object_id());
            map.put("black_type", page.getBlack_type());
          
            retInfo.setMark("0");
            retInfo.setTip("成功");
            retInfo.setObj(map);
            LogUtils.AdminOperation(redisTemplate ,tsJzAdminOperationDao,"查找黑名单" , request);

        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");

        return retInfo;
    }

    /**
     * @Purpose   添加黑名单
     * @param     tdBlack
     * @version   1.0
     * @author    李准
     * @time      2017-08-19
     * @return    RetInfo
     */
    public synchronized RetInfo  insertTdBlack(TdBlack tdBlack, HttpServletRequest request) {
        String logInfo = this.getClass().getName() + ":insertTdBlack:";
        RetInfo retInfo = new RetInfo();
        logger.info("======" + logInfo + "begin======");
        try {
            if (tdBlack.getBlack_type() == 1) {
                User user = userDao.findUserId(tdBlack.getBlack_object_id());
                if (user.getIs_auth() == 0 && user.getUser_type() == 1) {
                    TdBlack black = tdBlackDao.findTdBlack(tdBlack);
                    if (black != null ) {
                        if (black.getState() == 2) {
                            tdBlackDao.insertTdBlack(tdBlack);
                            retInfo.setMark("0");
                            retInfo.setTip("创建成功.");
                            LogUtils.AdminOperation(redisTemplate,tsJzAdminOperationDao,"添加黑名单,id:" + tdBlack.getBlack_id(),request);
                        } else {
                            retInfo.setMark("1");
                            retInfo.setTip("已存在未解除黑名单 不可添加");
                        }
                    } else {
                        tdBlackDao.insertTdBlack(tdBlack);
                        retInfo.setMark("0");
                        retInfo.setTip("创建成功.");
                        LogUtils.AdminOperation(redisTemplate,tsJzAdminOperationDao,"添加黑名单,id:" + tdBlack.getBlack_id(),request);
                    }
                } else {
                    retInfo.setMark("1");
                    retInfo.setTip("用户未实名认证，不可加入黑名单");
                }
            } else if (tdBlack.getBlack_type() == 2) {
                TdCompany tdCompany = tdCompanyDao.findTdCompany(tdBlack.getBlack_object_id());
                User user = userDao.findUserId(tdCompany.getUser_id());
                if (user.getIs_auth() == 0 && user.getUser_type() == 2) {
                    TdBlack black = tdBlackDao.findTdBlack(tdBlack);
                    if (black != null ) {
                        if (black.getState() == 2) {
                            tdBlackDao.insertTdBlack(tdBlack);
                            retInfo.setMark("0");
                            retInfo.setTip("创建成功.");
                            LogUtils.AdminOperation(redisTemplate,tsJzAdminOperationDao,"添加黑名单,id:" + tdBlack.getBlack_id(),request);
                        } else {
                            retInfo.setMark("1");
                            retInfo.setTip("已存在未解除黑名单 不可添加");
                        }
                    } else {
                        tdBlackDao.insertTdBlack(tdBlack);
                        retInfo.setMark("0");
                        retInfo.setTip("创建成功.");
                        LogUtils.AdminOperation(redisTemplate,tsJzAdminOperationDao,"添加黑名单,id:" + tdBlack.getBlack_id(),request);
                    }
                } else {
                    retInfo.setMark("1");
                    retInfo.setTip("用户未实名认证，不可加入黑名单");
                }
            } else {
                TdBlack black = tdBlackDao.findTdBlack(tdBlack);
                if (black != null ) {
                    if (black.getState() == 2) {
                        tdBlackDao.insertTdBlack(tdBlack);
                        retInfo.setMark("0");
                        retInfo.setTip("创建成功.");
                        LogUtils.AdminOperation(redisTemplate,tsJzAdminOperationDao,"添加黑名单,id:" + tdBlack.getBlack_id(),request);
                    } else {
                        retInfo.setMark("1");
                        retInfo.setTip("已存在未解除黑名单 不可添加");
                    }
                } else {
                    tdBlackDao.insertTdBlack(tdBlack);
                    retInfo.setMark("0");
                    retInfo.setTip("创建成功.");
                    LogUtils.AdminOperation(redisTemplate,tsJzAdminOperationDao,"添加黑名单,id:" + tdBlack.getBlack_id(),request);
                }
            }
        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
    /**
     * @Purpose   修改黑名单
     * @param     tdBlack
     * @version   1.0
     * @author    李准
     * @time      2017-08-19
     * @return    RetInfo
     */
    public synchronized RetInfo updateTdBlack(TdBlack tdBlack, HttpServletRequest request) {
        String logInfo = this.getClass().getName() + ":updateTdBlack:";
        RetInfo retInfo = new RetInfo();
        logger.info("======" + logInfo + "begin======");
        try {
            tdBlackDao.updateTdBlack(tdBlack);
            retInfo.setMark("0");
            retInfo.setTip("解除黑名单.");
            LogUtils.AdminOperation(redisTemplate,tsJzAdminOperationDao,"解除黑名单,id:" + tdBlack.getBlack_id(),request);


        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
}
